package com.wiz.weixin.controllor;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wiz.weixin.manager.TencentManager;
import com.wiz.weixin.service.StationService;
import com.wiz.weixin.service.WeixinConfigService;
import com.wiz.weixin.to.WeixinConfigTO;

/**
 * 油站介绍
 * @author xxy
 */
@Controller
@RequestMapping("/introduction")
@SuppressWarnings("all")
public class IntroduceControllor {
	
	@Resource
	private StationService stationService;
	@Resource
	private WeixinConfigService weixinConfigService;
	@Resource
	private TencentManager tencentManager;
	
	@RequestMapping(value="/listDescribe")
	public String listDescribe(HttpServletRequest request,HttpServletResponse response,
				HttpSession session, Model model) throws Exception {
		String openId = (String) session.getAttribute("openid");
		if (StringUtils.isBlank(openId)) {
			String code = request.getParameter("code");
			openId = tencentManager.getOpenId(code);
			session.setAttribute("openid", openId);
		}
		if(StringUtils.isBlank(openId)){
			model.addAttribute("msg", "未找到微信关联用户");
			return "/error";
		}
		//查询客户的所有会员
		List<String> hqIds =  (List<String>) session.getAttribute("hqIds");
		if(hqIds == null || hqIds.size()<= 0){
			hqIds = stationService.getHQIds(openId);
			session.setAttribute("hqIds", hqIds);
		}
		if(hqIds == null || hqIds.size()<= 0){
			model.addAttribute("msg", "对不起，您还未注册，请扫描加油站的二维码进行注册");
			return "/error";
		}
		List<WeixinConfigTO> list = weixinConfigService.listIntroduction(hqIds);
		model.addAttribute("list", list);
		return "/introduction/intro";
	}
}
